package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import po.User;
import utils.DBUtils;

public class UserDao {
    public User selectByUnamePasswordAndRole(User user) {
        User user1 = null;
        Connection connection = DBUtils.getConn();
        String sql = "select * from tb_user where uname=? and upwd=? and role=?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUname());
            preparedStatement.setString(2, user.getUpwd());
            preparedStatement.setInt(3, user.getRole());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                user1 = new User();
                user1.setUname(resultSet.getString("uname"));
                user1.setPhone(resultSet.getString("phone"));
                user1.setEmail(resultSet.getString("email"));
                user1.setUpwd(resultSet.getString("upwd"));
                user1.setRole(resultSet.getInt("role"));
            }
        } catch (SQLException var16) {
            var16.printStackTrace();
        } finally {
            DBUtils.close(preparedStatement, connection);

            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException var15) {
                var15.printStackTrace();
            }

        }

        return user1;
    }

    public User selectByUname(User user) {
        User user1 = null;
        Connection conn = DBUtils.getConn();
        String sql = "select * from tb_user where uname =? and role=?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, user.getUname());
            preparedStatement.setInt(2, user.getRole());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                user1 = new User();
                user1.setUname(resultSet.getString("uname"));
                user1.setPhone(resultSet.getString("phone"));
                user1.setEmail(resultSet.getString("email"));
                user1.setUpwd(resultSet.getString("upwd"));
                user1.setRole(resultSet.getInt("role"));
            }
        } catch (SQLException var16) {
            var16.printStackTrace();
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException var15) {
                var15.printStackTrace();
            }

            DBUtils.close(preparedStatement, conn);
        }

        return user1;
    }

    public User selectByEmailAndRole(User user) {
        User user1 = null;
        Connection conn = DBUtils.getConn();
        String sql = "select * from tb_user where email =? and role=?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, user.getEmail());
            preparedStatement.setInt(2, user.getRole());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                user1 = new User();
                user1.setUname(resultSet.getString("uname"));
                user1.setPhone(resultSet.getString("phone"));
                user1.setEmail(resultSet.getString("email"));
                user1.setUpwd(resultSet.getString("upwd"));
                user1.setRole(resultSet.getInt("role"));
            }
        } catch (SQLException var16) {
            var16.printStackTrace();
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException var15) {
                var15.printStackTrace();
            }

            DBUtils.close(preparedStatement, conn);
        }

        return user1;
    }

    public User selectByPhoneAndRole(User user) {
        User user1 = null;
        Connection conn = DBUtils.getConn();
        String sql = "select * from tb_user where phone =? and role=?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, user.getPhone());
            preparedStatement.setInt(2, user.getRole());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                user1 = new User();
                user1.setUname(resultSet.getString("uname"));
                user1.setPhone(resultSet.getString("phone"));
                user1.setEmail(resultSet.getString("email"));
                user1.setUpwd(resultSet.getString("upwd"));
                user1.setRole(resultSet.getInt("role"));
            }
        } catch (SQLException var16) {
            var16.printStackTrace();
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException var15) {
                var15.printStackTrace();
            }

            DBUtils.close(preparedStatement, conn);
        }

        return user1;
    }

    public int insert(User user1) {
        Connection connection = DBUtils.getConn();
        String sql = "insert into tb_user values(?,?,?,?,?)";
        PreparedStatement preparedStatement = null;
        int row = 0;

        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user1.getUname());
            preparedStatement.setString(2, user1.getUpwd());
            preparedStatement.setString(3, user1.getEmail());
            preparedStatement.setString(4, user1.getPhone());
            preparedStatement.setInt(5, user1.getRole());
            row = preparedStatement.executeUpdate();
        } catch (SQLException var10) {
            var10.printStackTrace();
        } finally {
            DBUtils.close(preparedStatement, connection);
        }

        return row;
    }
}
